﻿using System;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using Habanero.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using NUnit.Framework;

namespace FireStarterModeller.Test.Logic
{
    [TestFixture]
    public class TestLoadingEntity
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            TestUtilsLogic.SetupFixture();
        }

        [SetUp]
        public void SetupTest()
        {
            TestUtilsLogic.SetupTest();
        }

        [Ignore("Not sure what peter and hageshen where thinking")] //Brett 18 May 2009: Ignored Test - Not sure what peter and hageshen where thinking
        [Test]
        public void Test_SaveAndLoad_MemoryDatabase()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsLogic.CreateClassWithOneGuidProperty();

            DMProperty property1 = dmClass.Properties[0];
            dmClass.ClassNameBO = typeof(EntityStub).Name;
            dmClass.Assembly.AssemblyName = typeof(EntityStub).Assembly.GetName().Name;
            DMProperty property2 = TestUtilsLogic.AddPropertyWithNoRules(dmClass);
            ClassDefCol classDefs = new DmToClassDefsConverter().Convert((DMSolution) dmClass.Solution);
            IClassDef classDef = classDefs[dmClass.Assembly.AssemblyName, dmClass.ClassNameBO];
            EntityStub entity = new EntityStub((ClassDef) classDef);
            int value = TestUtilsShared.GetRandomInt();
            entity.SetPropertyValue(property2.PropertyName, value);
            entity.SetPropertyValue(property1.PropertyName, Guid.NewGuid());
            //---------------Execute Test ----------------------
            entity.Save();
            BusinessObjectManager.Instance.ClearLoadedObjects();
            EntityStub loadedEntity = (EntityStub)Broker.GetBusinessObject(classDef, entity.ID);
            //---------------Test Result -----------------------

            Assert.IsNotNull(loadedEntity);
            Assert.AreEqual
                (entity.GetPropertyValue(property2.PropertyName), loadedEntity.GetPropertyValue(property2.PropertyName));

            //---------------Tear Down -------------------------          
        }
    }
}